//检验手机号

function BlurMoblie() {
    var str = $('#mobile').val();
    var mbTest = /^(13|14|15|18|17)[0-9]{9}$/;
    if (!mbTest.test(str)) {
        tipsbox("手机号码格式不正确");
        return false;
    }
    else {
        return true;
    }
}

//检验验证码
function BlurCode() {
    var ecode = $("#ecode").val();
    if (ecode.length != 4) {
        tipsbox("请输入4位图形验证码");
        return false;
    }
    else {
        return true;
    }
}

//检验手机验证码
function BlurPhonecode() {
    var phonecode = $("#phonecode").val();
    if (phonecode.length != 6) {
        tipsbox("请输入6位短信验证码");
        return false;
    }
    else {
        return true;
    }
}


function checkecode(e) {
    var str = $(e).val();
    var mbTest = /^(13|14|15|18|17)[0-9]{9}$/;
    if (str.length == 4) {
        if (mbTest.test($('#mobile').val())) {
            $('#btnSendMsg').attr('onclick', 'sendMobileValidSMSCode()').removeClass('dis');
        }
    } else {
        if (mbTest.test($('#mobile').val())) {
            $('#btnSendMsg').attr('onclick', '').addClass('dis');
        }
    }
}

var timer = null;
var leftsecond = 60; //倒计时
var msg = "";

function sendMobileValidSMSCode() {
    var ecode = $("#ecode").val();
    var mobile = $("#mobile").val();
    tipsbox("短信发送中...");
    $("#btnSendMsg").removeAttr('onclick');
    $.ajax({
        url: sendcodeurl,
        type: "post",
        dataType: "json",
        data: {phone: mobile, verify: ecode},
        success: function (d) {
            leftsecond = 60;
            if (d.code == 1) {
                tipsbox(d.msg);
                clearInterval(timer);
                timer = setInterval(setLeftTime, 1000, "1");
                $("#btnSendMsg").val("请稍等..").addClass('dis');
                $("#mobile").attr("readonly", true);
            }
            else {
                tipsbox(d.msg);
                refreshEcode();
                $("#btnSendMsg").html("获取验证码");
                $("#mobile").attr("readonly", true);
            }
        }
    });
}

//刷新重新获取时间
function setLeftTime() {
    var second = Math.floor(leftsecond);
    $("#btnSendMsg").html(second + "s后重新发送");
    leftsecond--;
    if (leftsecond < 1) {
        clearInterval(timer);
        try {
            $("#btnSendMsg").html("获取验证码").removeClass('dis');
            $("#mobile").removeAttr("readonly");
            $("#btnSendMsg").attr({onclick: 'sendMobileValidSMSCode()',});
        } catch (E) {
        }
        return;
    }
}

//刷新验证码
function refreshEcode() {
    var e = $('.ecode');
    var src = e.attr('src');
    e.attr("src", src.replace(/\?.*$/, '') + '?' + Math.random());
    $('#ecode').val('');
}

$('.ecode').on('click', function () {
    refreshEcode();
})